const CONVERT_AK = '2xcjMVsZra93IhHC2rJyrpA1QkmtiDz3';

/**
 * 将 WGS84 坐标转换为百度地图 BD09 坐标
 * @param {number} lng 经度
 * @param {number} lat 纬度
 * @returns {Promise<{lng: number, lat: number}>} 转换后的 BD09 坐标
 */
export async function convertCoordinate(lng, lat) {
    try {
        // 使用代理，URL 前缀改为 /geoconv/v2/
        const url = `/geoconv/v2/?coords=${lng},${lat}&model=2&ak=${CONVERT_AK}`;
        const response = await fetch(url);

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const data = await response.json();
        if (data.status === 0 ) {
            return {
                lng: data.result[0].x,
                lat: data.result[0].y
            };
        } else {
            throw new Error('坐标转换失败');
        }
    } catch (error) {
        console.error('坐标转换错误:', error);
        throw error;
    }
}
// baiduConvert.js
export async function convertbc09llTobc09mm(lng, lat) {
    try {
        // 使用代理，URL 前缀改为 /geoconv/v2/
        const url = `/geoconv/v2/?coords=${lng},${lat}&model=3&ak=${CONVERT_AK}`;
        const response = await fetch(url);

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const data = await response.json();
        if (data.status === 0 ) {
            return {
                lng: data.result[0].x,
                lat: data.result[0].y
            };
        } else {
            throw new Error('坐标转换失败');
        }
    } catch (error) {
        console.error('坐标转换错误:', error);
        throw error;
    }
}

// 添加 BD09 转 WGS84 的方法
export function convertBD09ToWGS84(bdLng, bdLat) {
    // 百度坐标转火星坐标
    const gcjPoint = bd09togcj02(bdLng, bdLat);
    // 火星坐标转WGS84
    const wgsPoint = gcj02towgs84(gcjPoint[0], gcjPoint[1]);
    return { lng: wgsPoint[0], lat: wgsPoint[1] };
}

// 百度坐标系 (BD-09) 转 火星坐标系 (GCJ-02)
function bd09togcj02(lng, lat) {
    const x = lng - 0.0065;
    const y = lat - 0.006;
    const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);
    const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);
    const ggLng = z * Math.cos(theta);
    const ggLat = z * Math.sin(theta);
    return [ggLng, ggLat];
}

// 火星坐标系 (GCJ-02) 转 WGS84
function gcj02towgs84(lng, lat) {
    if (outOfChina(lng, lat)) {
        return [lng, lat];
    }
    let dLat = transformLat(lng - 105.0, lat - 35.0);
    let dLng = transformLng(lng - 105.0, lat - 35.0);
    const radLat = (lat / 180.0) * Math.PI;
    let magic = Math.sin(radLat);
    magic = 1 - 0.00669342162296594323 * magic * magic;
    const sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((6378245.0 * (1 - 0.00669342162296594323)) / (magic * sqrtMagic) * Math.PI);
    dLng = (dLng * 180.0) / (6378245.0 / sqrtMagic * Math.cos(radLat) * Math.PI);
    const mgLat = lat + dLat;
    const mgLng = lng + dLng;
    return [lng * 2 - mgLng, lat * 2 - mgLat];
}

// 判断是否在国内
function outOfChina(lng, lat) {
    return (
        lng < 72.004 || lng > 137.8347 ||
        lat < 0.8293 || lat > 55.8271
    );
}

function transformLat(x, y) {
    let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
    return ret;
}

function transformLng(x, y) {
    let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
    return ret;
}
// src/utils/wgs84ToBd09.js

function transformLat2(x, y) {
    const PI = Math.PI;
    let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin((y / 3.0) * PI)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin((y / 12.0) * PI) + 320 * Math.sin((y * PI) / 30.0)) * 2.0 / 3.0;
    return ret;
}

function transformLng2(x, y) {
    const PI = Math.PI;
    let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin((x / 4.0) * PI)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin((x / 12.0) * PI) + 300.0 * Math.sin((x / 30.0) * PI)) * 2.0 / 3.0;
    return ret;
}

export default function wgs84ToBd09(lng, lat) {
    const dLat = transformLat2(lng - 105.0, lat - 35.0);
    const dLng = transformLng2(lng - 105.0, lat - 35.0);
    const radLat = (lat / 180.0) * Math.PI;
    const magic = Math.sin(radLat);
    const sqrtMagic = Math.sqrt(1 - 0.00669342162296594323 * magic * magic);

    const deltaLat = (dLat * 180.0) / (((6378245.0 * (1 - 0.00669342162296594323)) / (magic * sqrtMagic)) * Math.PI);
    const deltaLng = (dLng * 180.0) / ((6378245.0 / sqrtMagic) * Math.cos(radLat) * Math.PI);

    return { lng: latlng[0] + deltaLng, lat: latlng[1] + deltaLat };
}
